<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\BankCard;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Swagger\Annotations as SWG;

class BankCardController extends Controller
{
    private $columns = ['id', 'card_no', 'bank_name', 'name'];

    /**
     * @SWG\Get(
     *     path="/front/bank-card",
     *     tags={"用户相关"},
     *     summary="银行卡",
     *     description="银行卡",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *              type="array",
     *              @SWG\Items(
     *                  @SWG\Property(property="id", type="integer", description="id"),
     *                  @SWG\Property(property="card_no", type="string", description="卡号"),
     *                  @SWG\Property(property="bank_name", type="string", description="银行"),
     *              )
     *          )
     *      )
     * )
     */
    public function index(Request $request, BankCard $bankCard)
    {
        $bankCard = $bankCard->where('user_id', $request->user()->id)->paginate(15, $this->columns);
        return $this->renderJson($bankCard->toArray());
    }

    /**
     * @SWG\Get(
     *     path="/front/bank-card/{id}",
     *     tags={"用户相关"},
     *     summary="单个银行卡",
     *     description="单个银行卡",
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", type="integer", description="卡id"),
     *     @SWG\Response(
     *          response="200",
     *          description="调用成功",
     *          @SWG\Schema(
     *              @SWG\Property(property="id", type="integer", description="id"),
     *              @SWG\Property(property="card_no", type="string", description="卡号"),
     *              @SWG\Property(property="bank_name", type="string", description="银行"),
     *          )
     *      )
     * )
     */
    public function show($id, BankCard $bankCard)
    {
        $bankCard = $bankCard->findOrFail($id);
        return $this->renderJson($bankCard);

    }

    /**
     * @SWG\Post(
     *     path="/front/bank-card",
     *     tags={"用户相关"},
     *     summary="添加银行卡",
     *     description="添加银行卡",
     *     consumes={"application/x-www-form-urlencoded"},
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="name", in="formData", required=true, type="string", description="持卡人"),
     *     @SWG\Parameter(name="bank_name", in="formData", required=true, type="string", description="银行"),
     *     @SWG\Parameter(name="card_no", in="formData", required=true, type="string", description="卡号"),
     *     @SWG\Parameter(name="card_no_configrmation", in="formData", required=true, type="string", description="重复卡号"),
     *     @SWG\Response(
     *          response="200",
     *          description="操作成功"
     *      )
     * )
     */
    public function store(Request $request, BankCard $bankCard)
    {
        $this->validate($request, [
            'name' => 'required',
            'bank_name' => 'required',
            'card_no' => 'required|confirmed|unique:bank_cards',
            'card_no_confirmation' => 'required'
        ], [
            'required' => ':attribute不能为空',
            'confirmed' => '银行卡号两次输入不一致',
            'card_no.unique' => '银行卡号已存在'
        ], [
            'bank_name' => '开户行',
            'card_no' => '银行卡号',
            'name' => '持卡人'
        ]);

        $client = new Client(['verify'=>false]);
        $card_no = request('card_no');
        $url = "http://ccdcapi.alipay.com/validateAndCacheCardInfo.json?cardNo={$card_no}&cardBinCheck=true";//6222005865412565805
        $response = $client->get($url);
        $responseData = json_decode((string)$response->getBody(), true);
        if ($responseData['validated'] != true){
            return $this->fail('卡号错误');
        }
        if ($responseData['bank'] != 'ICBC'){
            return $this->fail('该银行卡不是工商银行');
        }
        $data = [
            'user_id' => $request->user()->id,
            'card_no' => $card_no,
            'bank_name' => $request->input('bank_name'),
            'name' => $request->input('name')
        ];
        if ($bankCard->create($data)){
            return $this->success();
        }
        return $this->error('添加失败');
    }

    /**
     * @SWG\Put(
     *     path="/front/bank-card/{id}",
     *     tags={"用户相关"},
     *     summary="修改银行卡",
     *     description="修改银行卡",
     *     consumes={"application/x-www-form-urlencoded"},
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="string", description="id"),
     *     @SWG\Parameter(name="bank_name", in="formData", required=true, type="string", description="银行"),
     *     @SWG\Parameter(name="card_no", in="formData", required=true, type="string", description="卡号"),
     *     @SWG\Parameter(name="card_no_configrmation", in="formData", required=true, type="string", description="重复卡号"),
     *     @SWG\Response(
     *          response="200",
     *          description="操作成功"
     *      )
     * )
     */
    public function update(Request $request, $id, BankCard $bankCard)
    {
        $this->validate($request, [
            'bank_name' => 'required',
            'card_no' => 'required|confirmed',
            'card_no_confirmation' => 'required'
        ], [
            'required' => ':attribute不能为空',
            'confirmed' => '银行卡号两次输入不一致'
        ], [
            'bank_name' => '开户行',
            'card_no' => '银行卡号'
        ]);
        $data = [
            'card_no' => $request->input('card_no'),
            'bank_name' => $request->input('bank_name')
        ];
        $bankCard = $bankCard->findOrFail($id);
        if ($bankCard->update($data)){
            return $this->success();
        }
        return $this->error('修改失败');
    }

    /**
     * @SWG\Delete(
     *     path="/front/bank-card/{id}",
     *     tags={"用户相关"},
     *     summary="删除银行卡",
     *     description="删除银行卡",
     *     consumes={"application/x-www-form-urlencoded"},
     *     produces={"application/json"},
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Parameter(name="id", in="path", required=true, type="string", description="id"),
     *     @SWG\Response(
     *          response="200",
     *          description="操作成功"
     *      )
     * )
     */
    public function destroy($id, BankCard $bankCard)
    {
        $bankCard = $bankCard->findOrFail($id);
        try {
            $res = $bankCard->delete();
            if ($res){
                return $this->success();
            }
        } catch (\Exception $e) {
        }
        return $this->error('删除失败');
    }
}
